<?php

namespace plugin\admin\app\controller;

use app\services\PropertyService;
use plugin\admin\app\common\Auth;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Admin;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserPropertyLog;
use support\Request;
use support\Response;
use plugin\admin\app\model\UserRechargeLog;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 系统充值 
 */
class UserRechargeLogController extends Crud
{
    
    /**
     * @var UserRechargeLog
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new UserRechargeLog;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('user-recharge-log/index');
    }

    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());
        $password_filed = 'password';
        if (isset($data[$password_filed])) {
            $data[$password_filed] = Util::passwordHash($data[$password_filed]);
        }

        if (!Auth::isSupperAdmin() && $this->dataLimit) {
            if (!empty($data[$this->dataLimitField])) {
                $admin_id = $data[$this->dataLimitField];
                if (!in_array($admin_id, Auth::getScopeAdminIds(true))) {
                    throw new BusinessException('无数据权限');
                }
            }
        }

        $data['admin_id'] = admin_id();
        $data['created_time'] = date('Y-m-d H:i:s');

        return $data;
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {

            $operateType = $request->post('operate_type');

            if ($operateType == 'reduce') {
                if ($request->post('amount') <= 0) {
                    return $this->json(400, '扣除金额不能小于0');
                }
                PropertyService::reduce($request->post('user_id'), $request->post('type'), $request->post('amount'), UserPropertyLog::SYSTEM_REDUCE, $request->post('remark'));
            } else {
                if ($request->post('amount') <= 0) {
                    return $this->json(400, '充值金额不能小于0');
                }
                PropertyService::add($request->post('user_id'), $request->post('type'), $request->post('amount'), UserPropertyLog::RECHARGE, $request->post('remark'));
            }
            return parent::insert($request);
        }
        return view('user-recharge-log/insert', [
            'id' => $request->get('id'),
            'type' => $request->get('type')
        ]);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('user-recharge-log/update');
    }

    protected function formatNormal($items, $total): Response
    {
        $adminIds = array_column($items, 'admin_id');
        $admins = Admin::whereIn('id', $adminIds)->get()->toArray();
        $admins = array_combine(
            array_column($admins, 'id'),
            array_values($admins)
        );

        $userIds = array_column($items, 'user_id');
        $users = User::whereIn('id', $userIds)->get()->toArray();
        $users = array_combine(
            array_column($users, 'id'),
            array_values($users)
        );

        foreach ($items as $key=>$item) {
            $items[$key]['type_text'] = UserPropertyLog::getTypeText($item['type']);
            $items[$key]['admin_name'] = $admins[$item['admin_id']]['nickname'];
            $items[$key]['user_name'] = $users[$item['user_id']]['nickname'];
        }

        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }
}
